<template>
  <el-row v-if="showList">
    <el-col :span="5" style="padding-left: 20px; padding-top: 20px; border-right: 1px solid #dfe6ec">
      <customTree :ancestralId="treeQuery.ancestralId" :btnTitle="'  添加非结构化数据同步目录  '" @getId="getId" :type="true"
        @getTree="getTree" :expandType="'1'" />

      <!-- </div>
      </div> -->
    </el-col>
    <el-col :span="19">
      <div class="app-container jobPage">
        <div class="filter-container">
          <div class="filter-item">
            <span class="span">任务名称/描述 </span>
            <el-input v-model="listQuery.jobName" placeholder="请输入任务名称/描述" style="width: 200px" />
          </div>
          <!--          <div class="filter-item">-->
          <!--            <span class="span">调度类型 </span>-->
          <!--            <el-select v-model="listQuery.dbType" clearable placeholder="调度类型" style="width: 200px">-->
          <!--              <el-option-->
          <!--                v-for="item in dictList('datasource_type')"-->
          <!--                :key="'type' + item.label"-->
          <!--                :label="item.label"-->
          <!--                :value="item.value" />-->
          <!--            </el-select>-->
          <!--          </div>-->
          <div class="filter-item">
            <span class="span">执行状态 </span>
            <el-select v-model="listQuery.executeStatus" clearable placeholder="请选择任务状态" style="width: 160px">
              <el-option v-for="item in statusoptions" :key="'type' + item.label" :label="item.label"
                :value="item.value" />
            </el-select>
          </div>
          <!--          <div class="filter-item">-->
          <!--            <span class="span">执行状态 </span>-->
          <!--            <el-select v-model="listQuery.isSync" clearable placeholder="请选择执行状态" style="width: 100px">-->
          <!--              <el-option-->
          <!--                v-for="item in dictList('is_sync')"-->
          <!--                :key="'type' + item.label"-->
          <!--                :label="item.label"-->
          <!--                :value="item.value" />-->
          <!--            </el-select>-->
          <!--          </div>-->
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
            {{ $t('table.search') }}
          </el-button>
          <el-button v-waves plain v-permission="['metadata:source:add']" class="filter-item" style="margin-left: 10px"
            type="primary" icon="el-icon-plus" @click="handleSingle">
            新建非结构化数据同步
          </el-button>
          <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest')
            }}</el-button>

          <!--          <el-button v-waves v-permission="['metadata:source:delete']" class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleBathDelete">-->
          <!--            {{ $t('table.delete') }}-->
          <!--          </el-button>-->
          <!--          <el-button v-waves v-permission="['metadata:source:import']" class="filter-item" type="primary" icon="el-icon-upload2" @click="handleImport">-->
          <!--            {{ $t('table.import') }}-->
          <!--          </el-button>-->
          <!--          <el-button v-waves v-permission="['metadata:source:export']" :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">-->
          <!--            {{ $t('table.export') }}-->
          <!--          </el-button>-->
        </div>

        <el-table ref="table" :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row
          max-height="590" @row-dblclick="handleRowDoubleClick">
          <!--          @selection-change="handleSelectionChange"-->

          <!--          <el-table-column type="selection" width="55" />-->
          <!--          <el-table-column type="index" align="center" width="100" label="序号" />-->
          <el-table-column label="任务名称" align="center" min-width="150px" fixed="left">
            <template slot-scope="{ row }">
              <span>{{ row.jobName }}</span>
            </template>
          </el-table-column>

          <el-table-column label="任务描述" align="center" min-width="150px">
            <template slot-scope="{ row }">
              <span>{{ row.description }}</span>
            </template>
          </el-table-column>

          <el-table-column label="执行状态" align="center" min-width="100px">
            <template slot-scope="{ row }">
              <span v-if="row.executeStatus == 0">未运行</span>
              <span v-else-if="row.executeStatus == 1">正在运行</span>
              <span v-else-if="row.executeStatus == 2">运行结束</span>
            </template>
          </el-table-column>
          <el-table-column label="执行时长" align="center" min-width="100px">
            <template slot-scope="{ row }">
              <span>{{ row.executeDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数据源ip" align="center" min-width="180px">
            <template slot-scope="{ row }">
              <span>{{ row.sourceDatasourceIp }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数据源桶名称" align="center" min-width="180px">
            <template slot-scope="{ row }">
              <span>{{ row.sourceDatasourceBucket }}</span>
            </template>
          </el-table-column>
          <el-table-column label="目标源端口" align="center" min-width="180px">
            <template slot-scope="{ row }">
              <span>{{ row.targetDatasourcePort }}</span>
            </template>
          </el-table-column>
          <el-table-column label="目标源桶名称" align="center" min-width="180px">
            <template slot-scope="{ row }">
              <span>{{ row.targetDatasourceBucket }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width"
            fixed="right">
            <template slot-scope="{ row }">
              <!-- <el-button type="text" size="small" icon="el-icon-edit-outline"
                                @click="handleStopOrEdit(row)">编辑
                            </el-button> -->
              <!--              <el-button v-if="row.checkStatus==0"  type="text" size="small" icon="el-icon-video-play" style="margin-left: 10px;" @click="handleChangeJobStatus(row, '1', '启动')">开始</el-button>-->
              <!--              <el-button v-if="row.checkStatus==1"  type="text" size="small" icon="el-icon-video-pause" class="delete-text-btn" style="margin-left: 10px;" @click="handleChangeJobStatus(row, '0', '停止')">停止</el-button>-->
              <el-button size="small" type="text" icon="el-icon-video-play" @click="runJobNow(row)">执行</el-button>
              <el-button v-permission="['dataextraction:DataExtraction:update']" type="text" size="small"
                icon="el-icon-edit" @click="handleTableEdit(row)">
                {{ $t('table.edit') }}
              </el-button>
              <el-button v-permission="['dataextraction:DataExtraction:delete']" type="text" size="small"
                icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row)">
                {{ $t('table.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-col>
    <addroedit v-if="show" :editdata="editdata" :options="treeList" @change="errorchange" />
  </el-row>
</template>

<script>
import { fetchMasterdataDirectoryList, deleteMasterdataDirectory, delDataextractionJob, startOneForSpark, getDataextractionJobList } from '@/api/masterdata/modelmasterdataDirectory.js'
import Sortable from 'sortablejs'
import addroedit from './addoredit/index'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import customTree from '@/components/customTree/index.vue'
export default {
  components: { addroedit, customTree },
  directives: { waves, permission },

  data() {
    return {
      show: false,
      show: false,
      showList: true,
      tableKey: 0,
      statusoptions: [
        {
          value: 0,
          label: '未运行',
        },
        {
          value: 1,
          label: '正在运行',
        },
      ],
      list: [],
      total: 0,
      multipleSelection: [],
      downloadLoading: false,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      editdata: {},
      listQuery: {
        jobName: undefined,
        dbType: undefined,
        executeStatus: undefined,
        checkStatus: undefined,
        isSync: undefined,
        directoryId: '',
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
      treeQuery: {
        tableName: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
        ancestralId: '13',
      },
      treeList: [],
      NODE_KEY: 'id', // id对应字段
      MAX_LEVEL: 5, // 设定最大层级
      NODE_ID_START: 0, // 新增节点id，逐次递减
      startId: null,
      defaultProps: {
        // 默认设置
        children: 'children',
        label: 'name',
      },
      initParam: {
        // 新增参数
        name: '新增节点',
        parentId: 0,
        children: [],
      },
      currentNodekey: '',
      dataToSend: '',
    }
  },

  methods: {
    getId(data) {
      this.listQuery.ancestralId = '13'
      this.listQuery.directoryId = data.id
      this.getList()
    },
    getTree(data) {
      this.treeList = data
    },

    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delDataextractionJob(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success("删除成功")
          } else {
            this.$message.error("删除失败")
          }
        })
      })
    },
    runJobNow(row) {
      startOneForSpark({ id: row.id }).then((res) => {
        if (res.data.code == 0) {
          this.$message.success(res.data.msg)
          this.getList()
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    errorchange() {
      this.getList()
      this.show = false
    },
    handleSingle() {
      this.dataToSend = 'create'
      this.editdata = { addroedit: 'add' }

      // this.$refs.single.open()
      this.show = true
    },
    handleRowDoubleClick(row, column, event) {
      // 处理双击事件
      console.log('双击了行', row)
      getDataextractionJobDetail(row.id).then((response) => {
        if (response.data.code === 0) {
          this.$message.success('获取详细信息成功')
          // this.$refs.getForm.open(response.data.data)
          // this.show = true
          // this.editdata = { ...row, addroedit: 'edit' }
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },

    handleStopOrEdit(row) {
      this.show = true
      this.editdata = { ...row, addroedit: 'edit' }
    },
    handleFilter() {
      if (this.listQuery.directoryId) {
        this.listQuery.page = 1
        this.getList()
      } else {
        this.$message.warning('请选择目录')
      }
    },

    //重置
    handleRest() {
      this.listQuery = {
        directoryId: this.listQuery.directoryId,
        page: this.listQuery.page,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      getDataextractionJobList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.show = false
        this.setSort()
        this.listLoading = false
      })
    },
    setSort() {
      const el = this.$refs.table.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        setData: function (dataTransfer) {
          // to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
          dataTransfer.setData('Text', '')
        },
        onEnd: (evt) => {
          const targetRow = this.list.splice(evt.oldIndex, 1)[0]
          this.list.splice(evt.newIndex, 0, targetRow)
          // TODO：这里完成自己的业务处理
        },
      })
    },

    handleLook(row) {
      this.$refs.form.handleLook(row.id)
    },

    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delDataextractionJob(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleTableEdit(row) {
      console.log(row, 'handleTableEdit')
      this.show = true
      this.editdata = { ...row, addroedit: 'edit' }
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}

::v-deep(.el-table--border ::-webkit-scrollbar) {
  display: block !important;
}

.jobPage .el-dialog {
  margin-top: 0px !important;
  height: 100% !important;
  width: 100% !important;
}

.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 90px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}

.el-row {
  display: flex;
}

.el-col-5 {
  flex: 1;
}
</style>
